
export default {
	namespaced: true,
	state:{
		openid: null,
		info: {},
		access_token: uni.getStorageSync('access_token') || null ,//登陆token
		isLogin: null, //判断用户是否已经登陆
		showLoginModal: false,
		endVal: 5000,
		myInfo: {},
		myWallet: {}
	},
	mutations: {
		//全局任何地方弹出登陆模态框
		onShowLoginModal(state){
			state.showLoginModal = true;
		}
	},
	actions: {
		//当页面需要用户登陆的时候进行路由拦截，并做相应的处理
		//用户登陆拦截
		onLoginIntercept({state,commit}){
			if(!state.isLogin){
				const title = '请先登录';
				commit('onShowLoginModal')
				uni.showToast({
					icon: 'none',
					title
				});
				throw new Error(title)
			}
		},
		async loginSuccess({state,dispatch}){
			await dispatch('getUserInfo');
			await dispatch('getMyInfo');
			await dispatch('getMyWallet');
		},
		async getUserInfo({state}){
			let res = await uni.$u.api.getMyHome();
			state.info = res.user;
		},
		async getMyInfo({state}){
			let res = await uni.$u.api.getMyInfo();
			state.myInfo = res;
		},
		async getMyWallet({state}){
			let res = await uni.$u.api.getMyWallet();
			state.myWallet = res;
		}
	}
}